//
//  LXDevSettingViewController.m
//  bgms_d
//
//  Created by Mountain on 1/3/2019.
//  Copyright © 2019 lachesis-zhu. All rights reserved.
//

#import "LXDevSettingViewController.h"
#import "LXAlertInViewController.h"
#import "LXConnectViewController.h"
#import "LXHomeViewController.h"
#import "LXSwitchView.h"

@interface LXDevSettingViewController () <UITableViewDelegate, UITableViewDataSource, LXSwitchViewDelegate>
{
    LXDevSettingModel *devSetting;
}

@property (nonatomic, strong) NSArray *dataArray;

@end

@implementation LXDevSettingViewController

#pragma mark -  public method

#pragma mark -  life cycle
- (void)dealloc
{
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initializationData];
    [self addNotification];
    
    [self loadUI];
    
    [self loadViewDatas];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewDidAppear:(BOOL)animated
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
    if (indexPath) {
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        UILabel *titleLabel = [cell.accessoryView viewWithTag:1];
        titleLabel.text = devSetting.alertInterval;
    }
}

- (void)viewDidDisappear:(BOOL)animated
{
    NSDictionary *dev = @{
                          @"tempUnitF" : @(devSetting.tempUnitF),
                          @"connectNotif" : @(devSetting.connectNotif),
                          @"alertInterval" : devSetting.alertInterval,
                          @"alertRingtone" : @(devSetting.alertRingtone),
                          @"alertVibration" : @(devSetting.alertVibration),
                          };
    [self saveObject:dev forKey:@"DevSetting"];
    self.navigationController.navigationBar.hidden = NO;
}

#pragma mark -  over write

#pragma mark -  setter & getter

#pragma mark -  private method
- (void)initializationData
{
    devSetting = [LXDevSettingModel shareSettingModel];
    self.dataArray =
  @[
       @{@"footName" : NSLocalizedString(@"ThePhoneWillNotify", nil),
         @"cellData" : @[@{@"image" : @"deviceIcon1", @"name" : NSLocalizedString(@"TemperatureUnit", nil)},
                         @{@"image" : @"deviceIcon2", @"name" : NSLocalizedString(@"ConnectionNotification", nil)}]},
       @{@"footName" : NSLocalizedString(@"IntervalTimeBetween", nil),
         @"cellData" : @[@{@"image" : @"deviceIcon3", @"name" : NSLocalizedString(@"AlarmInterval", nil)}]},
       @{@"footName" : NSLocalizedString(@"IfEnabledYourPhone", nil),
         @"cellData" : @[@{@"image" : @"deviceIcon4", @"name" : NSLocalizedString(@"AlarmRingtone", nil)},
                         @{@"image" : @"deviceIcon5", @"name" : NSLocalizedString(@"AlarmVibration", nil)}]},
    ];
}

- (void)loadUI
{
    self.view.backgroundColor = kAppBackgroundColor;
    [self buildNavi];

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)loadViewDatas
{
    
}

- (void)buildNavi
{
    [self.navigationView drawAroundViewWithLocation:DrawBottom color:kAppLinesColor insets:UIEdgeInsetsMake(-0.5, 0, 0, 0)];
    if (IPHONE_X) {
        self.naviHeight.constant = 66 + 22;
    }
//    [self.navigationView drawAroundViewWithLocation:DrawBottom color:kAppLinesColor insets:UIEdgeInsetsMake(-0.5, 0, 0, 0)];
//    UIButton *deleteBtn = [[UIButton alloc] init];
//    [deleteBtn setImage:[UIImage imageNamed:@"垃圾桶"] forState:UIControlStateNormal];
//    [deleteBtn addTarget:self action:@selector(deleteDown:) forControlEvents:UIControlEventTouchUpInside];
//    UIBarButtonItem *deleteBar = [[UIBarButtonItem alloc] initWithCustomView:deleteBtn];
//    self.navigationItem.rightBarButtonItem = deleteBar;
}

#pragma mark -  notification
- (void)addNotification
{
    
}

#pragma mark -  network


#pragma mark -  event handle
- (IBAction)deleteDown:(UIButton *)sender
{
    [[BLETransDeviceSingleton sharedInstance] disConnect];
    [self saveObject:@"" forKey:BLUENAME];
    
    NSArray *viewCcontrollers = [self.navigationController viewControllers];
    LXHomeViewController *homeVC = (LXHomeViewController *)[viewCcontrollers objectAtIndex:viewCcontrollers.count - 2];
    homeVC = nil;
    LXConnectViewController *vc = [[LXConnectViewController alloc] initWithNibName:NSStringFromClass([LXConnectViewController class]) bundle:nil];
    LXThemeColorNavigationController *themeNav = [[LXThemeColorNavigationController alloc] initWithRootViewController:vc];
    self.view.window.rootViewController = themeNav;
}

- (IBAction)backDown:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)switchDown:(UISwitch *)sender
{
    NSInteger index = sender.tag;
    switch (index) {
        case 1:
            devSetting.connectNotif = sender.isOn;
            break;
        case 20:
            devSetting.alertRingtone = sender.isOn;
            break;
        case 21:
            devSetting.alertVibration = sender.isOn;
            break;
        default:
            break;
    }
}
#pragma mark -  delegate
#pragma mark
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, [LXAutoLength lx_L:64])];
    footView.backgroundColor = kAppBackgroundColor;
    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(15, 5, kScreenWidth - 30, [LXAutoLength lx_L:64] - 10)];
    name.font = [UIFont lx_systemFontOfSize:15];
    name.textColor = kAppGrayBgColor;
    NSDictionary *dic = [self.dataArray objectAtIndex:section];
    name.text = dic[@"footName"];
    name.numberOfLines = 2;
    name.lineBreakMode = NSLineBreakByTruncatingTail;
    [footView addSubview:name];
    return footView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return [LXAutoLength lx_L:64];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [LXAutoLength lx_L:54];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSDictionary *dic = [self.dataArray objectAtIndex:section];
    NSArray *cells = dic[@"cellData"];
    return cells.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    NSDictionary *dic = [self.dataArray objectAtIndex:indexPath.section];
    NSArray *arrays = dic[@"cellData"];
    NSDictionary *cellDic = [arrays objectAtIndex:indexPath.row];
    cell.imageView.image = [UIImage imageNamed:cellDic[@"image"]];
    cell.textLabel.text = cellDic[@"name"];
    cell.textLabel.textColor = kAppNormalTextColor;
    if (indexPath.row == 0) {
        [cell drawAroundViewWithLocation:DrawBottom color:kAppLinesColor insets:UIEdgeInsetsMake(0, 0, 0.5, 0)];
    }
    if (indexPath.section == 0) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (indexPath.row == 0) {
            LXSwitchView *switchView = [[LXSwitchView alloc] initWithFrame:CGRectMake(0, [LXAutoLength lx_L:12], [LXAutoLength lx_L:60], [LXAutoLength lx_L:30])];
            switchView.delegate = self;
            cell.accessoryView = switchView;
            if (devSetting.tempUnitF) {
                switchView.fButton.selected = YES;
                switchView.cButton.selected = NO;
            } else {
                switchView.cButton.selected = YES;
                switchView.fButton.selected = NO;
            }
        } else {
            UISwitch *switchBtn = [[UISwitch alloc] init];
            switchBtn.on = YES;
            cell.accessoryView = switchBtn;
            switchBtn.onTintColor = kAppSwitchOnCloor;
            switchBtn.tintColor = kAppSwitchOnCloor;
            switchBtn.tag = indexPath.section * 10 + indexPath.row;
            [switchBtn addTarget:self action:@selector(switchDown:) forControlEvents:UIControlEventValueChanged];
            switchBtn.on = devSetting.connectNotif;
        }
    } else if (indexPath.section == 1) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        UIView *nextView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
        UILabel *showLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
        showLabel.font = [UIFont lx_systemFontOfSize:16];
        showLabel.textColor = kAppGrayBgColor;
        showLabel.textAlignment = NSTextAlignmentRight;
        showLabel.text = devSetting.alertInterval;
        showLabel.tag = 1;
        UIImageView *showImage = [[UIImageView alloc] initWithFrame:CGRectMake(85, 12, 10, 16)];
        showImage.image = [UIImage imageNamed:@"Chevron"];
        [nextView addSubview:showLabel];
        [nextView addSubview:showImage];
        cell.accessoryView = nextView;
    } else if (indexPath.section == 2) {
        UISwitch *switchBtn = [[UISwitch alloc] init];
        switchBtn.on = YES;
        cell.accessoryView = switchBtn;
        switchBtn.onTintColor = kAppSwitchOnCloor;
        switchBtn.tintColor = kAppSwitchOnCloor;
        switchBtn.tag = indexPath.section * 10 + indexPath.row;
        [switchBtn addTarget:self action:@selector(switchDown:) forControlEvents:UIControlEventValueChanged];
        if (indexPath.row == 0) {
            switchBtn.on = devSetting.alertRingtone;
        } else {
            switchBtn.on = devSetting.alertVibration;
        }
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.section == 1) {
        LXAlertInViewController *vc = [[LXAlertInViewController alloc] initWithNibName:NSStringFromClass([LXAlertInViewController class]) bundle:nil];
        vc.selectString = self.selectString;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - LXSwitchViewDelegate
- (void)switchView:(LXSwitchView *)view switchDown:(UIButton *)btn
{
    if (btn.tag == 0) {
        if (btn.isSelected) {
            devSetting.tempUnitF = YES;
        } else {
            devSetting.tempUnitF = NO;
        }
    } else {
        if (btn.isSelected) {
            devSetting.tempUnitF = NO;
        } else {
            devSetting.tempUnitF = YES;
        }
    }
    [[BLETransDeviceSingleton sharedInstance] secretKeySendTimeAlert:[[LXDevSettingModel shareSettingModel].alertInterval integerValue] boundsAlert:0 alertOn:YES];
}
#pragma mark -  lazy load


#pragma mark -  memoryWarning

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
